package com.shwread.android.qysw.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.shwread.android.qysw10000213.R;
import com.shwread.android.ui.widget.LoadingView;
import com.shwread.httpsdk.http.callback.BaseAction;
import com.shwread.httpsdk.http.face.QryCommentPraiseNumAction;

import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;
import logic.bean.AudioAndVideo;
import logic.bean.NumberInfo;
import logic.bean.RegionContentItemBean;
import logic.constants.DefaultConsts;
import logic.util.DateUtil;
import logic.util.NetworkUtil;

/**
 * Created by lex on 2017/2/21.
 */

public class VideoDetailActivity extends BaseSimpleActivity {


    private LoadingView loadingView;
    private TextView tv_video_title, tv_video_date, tv_video_desc, video_bottom_comment_textview;
    private JCVideoPlayerStandard player_list_video;
    private LinearLayout video_bottom_tools_lay;
    private RelativeLayout video_bottom_comment, video_top_title_lay;
    private ImageView  video_top_title_back_btn;
    private RegionContentItemBean contentItemBean;
    private AudioAndVideo audioAndVideo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.video_detail);
        contentItemBean = (RegionContentItemBean) getIntent().getSerializableExtra("video");
        if (null != contentItemBean) {
            audioAndVideo = contentItemBean.getVideoNews();
        } else {
            finish();
        }
        initView();
        setListener();
        initLoadingView();
        setData();
    }

    private void setData() {
        tv_video_title.setText(audioAndVideo.getTitle());
        tv_video_date.setText(DateUtil.getRegionTime(contentItemBean.getPublishTime()));
        tv_video_desc.setText(audioAndVideo.getShortDesc());
//        video_bottom_comment_textview.setText(audioAndVideo.getCommentNum()+"");
        player_list_video.setUp(
                audioAndVideo.getHtmlUrl(),
                JCVideoPlayer.SCREEN_LAYOUT_LIST,
                audioAndVideo.getTitle(), false);
        Glide.with(this).load(audioAndVideo.getCoverImageUrl()).into(player_list_video.thumbImageView);
    }

    void initView() {
        loadingView = (LoadingView) findViewById(R.id.video_loading_view);
        tv_video_title = (TextView) findViewById(R.id.tv_video_title);
        tv_video_date = (TextView) findViewById(R.id.tv_video_date);
        tv_video_desc = (TextView) findViewById(R.id.tv_video_desc);
        video_bottom_comment_textview = (TextView) findViewById(R.id.video_bottom_comment_textview);
        player_list_video = (JCVideoPlayerStandard) findViewById(R.id.player_list_video);
        video_bottom_tools_lay = (LinearLayout) findViewById(R.id.video_bottom_tools_lay);
        video_bottom_comment = (RelativeLayout) findViewById(R.id.video_bottom_comment);
        video_top_title_lay = (RelativeLayout) findViewById(R.id.video_top_title_lay);
//        video_bottom_comment_imgv = (ImageView) findViewById(R.id.video_bottom_comment_imgv);
        video_top_title_back_btn = (ImageView) findViewById(R.id.video_top_title_back_btn);

        player_list_video.tinyBackImageView.setVisibility(View.GONE);
        player_list_video.backButton.setVisibility(View.GONE);
        player_list_video.titleTextView.setVisibility(View.GONE);
        player_list_video.thumbImageView.setScaleType(ImageView.ScaleType.FIT_XY);
    }

    void setListener() {
        video_top_title_back_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                JCVideoPlayer.releaseAllVideos();
                finish();
            }
        });

        TextView tvTitle = (TextView) findViewById(R.id.common_header_name);
        tvTitle.setText("");
        Button common_header_back = (Button) findViewById(R.id.common_header_back);
        common_header_back.setVisibility(View.VISIBLE);
        common_header_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                JCVideoPlayer.releaseAllVideos();
                finish();
            }
        });

        video_bottom_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent();
                intent1.setClass(VideoDetailActivity.this, CommentListActivity.class);
                intent1.putExtra(DefaultConsts.COMMENT_TARGET_TYPE, "2");
                intent1.putExtra(DefaultConsts.COMMENT_TARGET_VALUE, contentItemBean.getVideoNews().getModelContentDetailId()+"");
                startActivity(intent1);
            }
        });
    }

    private void initLoadingView() {
        loadingView.setLoadingAnim(R.drawable.home_progress);
        loadingView.changeLoadingStatus(0);
        loadingView.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (NetworkUtil.isNetAvailable(this)) {
            new QryCommentPraiseNumAction(this, "2", contentItemBean.getVideoNews().getModelContentDetailId()+"",
                    new PraiseNumListener()).start();
        }
    }

    @Override
    public void onBackPressed() {
        if (JCVideoPlayer.backPress()) {
            return;
        }else{
            JCVideoPlayer.releaseAllVideos();
            super.onBackPressed();
        }
    }

    private class PraiseNumListener
            extends
            BaseAction {
        @Override
        public void OK(Object obj) {
            final NumberInfo info = (NumberInfo) obj;
            Log.e("评论数",obj.toString()+"-----------"+info.commentNum);
            VideoDetailActivity.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (null != info) {
                        video_bottom_comment_textview.setText(info.commentNum == 0 ? "" : String.valueOf(info.commentNum));
                    }
                }
            });
        }
    }

}
